import { axiosInstance } from './axios-instance';

const authApi = {
  login: async (username: string, password: string) => {
    try {
      const response = await axiosInstance.post('/auth/login', {
        username,
        password,
      });
      const { access_token, user } = response.data;
      localStorage.setItem('token', access_token);
      localStorage.setItem('user', JSON.stringify(user));
      return response.data;
    } catch (error) {
      throw error;
    }
  },

  register: async (username: string, email: string, password: string, role: string = 'STUDENT') => {
    try {
      const response = await axiosInstance.post('/auth/register', {
        username,
        email,
        password,
        role,
      });
      const { access_token, user } = response.data;
      localStorage.setItem('token', access_token);
      localStorage.setItem('user', JSON.stringify(user));
      return response.data;
    } catch (error) {
      throw error;
    }
  },
};

export default authApi;